package cn.cocho.android.mc.storage;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import cn.cocho.android.mc.util.CommonContexts;


/**
 * 应用的内存管理（记录到内存卡上）
 * 
 * @author shk
 * 
 */
public class ApplicationResource {


	/**
	 * 读取数据库的脚本文件
	 * 
	 * @param assets目录下的文件
	 * @return 脚本内容或null
	 * @author HAIKANG SONG
	 * @time 2013-5-6下午7:00:37
	 */
	public String readAssetsFile(String assetsFilePath) {
		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		InputStream in = null;
		try {
			in = ApplicationCache.context.getResources().getAssets().open(assetsFilePath);//获得assets目录下的文件
			if (in != null) {
				br = new BufferedReader(new InputStreamReader(in, CommonContexts.encode));
				String line = br.readLine();
				while (line != null) {
					sb.append(line);
					line = br.readLine();
				}
			} else {
				return null;
			}
		} catch (UnsupportedEncodingException e) {
			throw new IllegalArgumentException("read assets file[" + assetsFilePath + "] is error!", e);
		} catch (FileNotFoundException e) {
			throw new IllegalArgumentException("read assets file[" + assetsFilePath + "] is error!", e);
		} catch (IOException e) {
			throw new IllegalArgumentException("read assets file[" + assetsFilePath + "] is error!", e);
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (br != null) {
					br.close();
				}
			} catch (IOException e) {
			}
		}
		return sb.toString();
	}

}
